﻿using JESAI.Redis.Datas;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Numerics;
using System.Text;

namespace JESAI.Redis.Helpers
{
    internal static class EncodingHelper
    {
        /// <summary>
        /// 对象转字节数组
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this object obj)
        {
            return obj switch
            {
                string s => Encoding.UTF8.GetBytes(s),
                byte[] b => b,
                _ => Encoding.Default.GetBytes(obj.ToString())
            };
        }

        /// <summary>
        /// 编码 池化
        /// </summary>
        /// <param name="obj"></param>
        public static RedisEncodeData ToEncodeData(this object obj)
        {
            switch (obj)
            {
                case byte[] bytes:
                    return new RedisEncodeData(bytes, bytes.Length, false);
                case string str:
                    {
                        var reusableBuffer = ArrayPool<byte>.Shared.Rent(str.Length * 3);

                        var length = Encoding.UTF8.GetBytes(str, 0, str.Length, reusableBuffer, 0);

                        return new RedisEncodeData(reusableBuffer, length, true);
                    }
            }

            var newStr = obj + "";
            var reusableBuffer2 = ArrayPool<byte>.Shared.Rent(newStr.Length * 3);

            var length2 = Encoding.UTF8.GetBytes(newStr, 0, newStr.Length, reusableBuffer2, 0);

            return new RedisEncodeData(reusableBuffer2, length2, true);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string source)
        {
            return string.IsNullOrWhiteSpace(source);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static int ToInt(this string source)
        {
            int.TryParse(source, out var res);
            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool ToBool(this string source)
        {
            bool.TryParse(source, out var res);
            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static long ToLong(this string source)
        {
            long.TryParse(source, out var res);
            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static BigInteger ToBigInteger(this string source)
        {
            BigInteger.TryParse(source, out var res);
            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static double Todouble(this string source)
        {
            double.TryParse(source, out var res);
            return res;
        }
    }
}
